package org.assistant.common.jdbc.datasource;

import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.Properties;

import javax.sql.DataSource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.assistant.common.utils.ClassUtils;

public abstract class AbstractDriverBasedDataSource implements DataSource {
	protected final static Log log = LogFactory
		.getLog(AbstractDriverBasedDataSource.class);
	protected final static boolean isDebugEnabled = log.isDebugEnabled();

	private String url;
	private String username;
	private String password;
	private Properties connectionProperties;

	public PrintWriter getLogWriter() throws SQLException {
		throw new UnsupportedOperationException("getLogWriter");
	}

	public int getLoginTimeout() throws SQLException {
		return 0;
	}

	public void setLogWriter(PrintWriter out) throws SQLException {
		throw new UnsupportedOperationException("setLogWriter");
	}

	public void setLoginTimeout(int seconds) throws SQLException {
		throw new UnsupportedOperationException("setLoginTimeout");
	}

	public boolean isWrapperFor(Class<?> iface) throws SQLException {
		return DataSource.class.equals(iface);
	}

	@SuppressWarnings("unchecked")
	public <T> T unwrap(Class<T> iface) throws SQLException {
		if (iface == null) {
			throw new IllegalArgumentException(
				"Interface argument must not be null");
		}
		if (!DataSource.class.equals(iface)) {
			throw new SQLException(
				"DataSource of type ["
						+ getClass().getName()
						+ "] can only be unwrapped as [javax.sql.DataSource], not as ["
						+ iface.getName());
		}
		return (T) this;
	}

	public void setDriverClassName(String driverClassName) {
		if (driverClassName == null || "".equals(driverClassName.trim())) {
			throw new IllegalArgumentException(
				"Property 'driverClassName' must not be empty");
		}
		String driverClassNameToUse = driverClassName.trim();
		try {
			Class.forName(driverClassNameToUse, true, ClassUtils
				.getDefaultClassLoader());
		} catch (ClassNotFoundException ex) {
			IllegalStateException ise = new IllegalStateException(
				"Could not load JDBC driver class [" + driverClassNameToUse
						+ "]");
			ise.initCause(ex);
			throw ise;
		}
		if (log.isInfoEnabled()) {
			log.info("Loaded JDBC driver: " + driverClassNameToUse);
		}
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		if (url == null || "".equals(url.trim())) {
			throw new IllegalArgumentException(
				"Property 'url' must not be empty");
		}
		this.url = url.trim();
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Properties getConnectionProperties() {
		return connectionProperties;
	}

	public void setConnectionProperties(Properties connectionProperties) {
		this.connectionProperties = connectionProperties;
	}
}
